package com.thinkfit.jy.utils;

import androidx.core.content.ContextCompat;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.thinkfit.jy.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 柱状图
 */
public class HistogramUtils {

    private static boolean isShowText = false;

    static HistogramUtils histogramUtils;

    public static HistogramUtils getInstance(){
        if(histogramUtils == null){
            histogramUtils = new HistogramUtils();
        }

        return histogramUtils;
    }

    public void initBiao(BarChart chart){

        chart.getDescription().setEnabled(false);

        //x轴设置
        XAxis xAxis = chart.getXAxis();//获取x轴
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置
        xAxis.setDrawGridLines(false);//不绘制格网线
        xAxis.setGranularity(0f);//设置最小间隔，防止当放大时，出现重复标签。
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawLabels(false);
        xAxis.setCenterAxisLabels(false);

        chart.getAxisLeft().setEnabled(false);
//        xAxis.setValueFormatter(xAxisFormatter);
//        xAxis.setTextSize(xAxisTextSize);//设置标签字体大小
//        xAxis.setLabelCount(xAxisValue.size());//设置标签显示的个数

        //y轴设置
        YAxis leftAxis = chart.getAxisLeft();//获取左侧y轴
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧
        leftAxis.setAxisMinimum(0f);//设置Y轴最小值
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawLabels(false);//禁止绘制y轴标签
        leftAxis.setDrawAxisLine(false);//禁止绘制y轴

        chart.getAxisRight().setEnabled(false);//禁用右侧y轴


        // 图例设置
        Legend legend = chart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向
        legend.setForm(Legend.LegendForm.EMPTY);//图例窗体的形状
        chart.setDrawValueAboveBar(true);

        //legend.setYOffset(-2f);


        chart.setExtraBottomOffset(10);//距视图窗口底部的偏移，类似与paddingbottom
        chart.setExtraTopOffset(30);//距视图窗口顶部的偏移，类似与paddingtop
        chart.setFitBars(true);//使两侧的柱图完全显示
//        chart.animateX(1500);//数据显示动画，从左往右依次显示




        chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {

            }

            @Override
            public void onNothingSelected() {

            }
        });
    }

    public void setBarChartData(BarChart barChart, List<Float> yAxisValue, String title, int... colors) {

        ArrayList<BarEntry> entries = new ArrayList<>();

        for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
            entries.add(new BarEntry(i, yAxisValue.get(i)));
        }
        BarDataSet set1;
        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            System.out.println("ddddddddddddddddddddddd   set111111");
            set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set1.setValues(entries);
            set1.setDrawValues(isShowText);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            System.out.println("ddddddddddddddddddddddd   set222222 : "+colors);
            set1 = new BarDataSet(entries, title);
            if (colors.length <= 0) {
                set1.setColor(ContextCompat.getColor(barChart.getContext(), R.color.color_5F5C5F));//设置set1的柱的颜色
            } else {
                set1.setColors(colors);
            }
            set1.setDrawValues(isShowText);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setBarWidth(0.9f);
//            data.setValueFormatter(new ValueFormatter());

            barChart.setData(data);
            barChart.notifyDataSetChanged();
        }
    }

    /**
     * 是否显示图上的数值
     * @param b
     */
    public void setDrawValues(boolean b){
        isShowText = b;
    }
}
